define([],function(){
	var order;
    return {
        couponObj:{},
        setTicket: function(step, couponid){
	        var thisCoupon = this.couponObj[couponid],
	            goodsprice = config.goods.goodsprice,
	            orderamount = amount.getNumber(),
	            allfacevalue = this.getTotal();
	
	        if ((thisCoupon.num==thisCoupon.max&&step > 0)||(allfacevalue >= goodsprice * orderamount && step > 0)||(step<0&&this.couponObj[couponid].num==0)) {
	            return false;
	        }
	
	        thisCoupon.num=thisCoupon.num+step;
	        
	        $("#couponNumCt_" + couponid).html(thisCoupon.num);
	        $("#couponlostCt_" + couponid).html(thisCoupon.max-thisCoupon.num);
	       
	        order.upDateUI();
	    },
	    remove:function(couponid){
	    	var ct=$('#couponWraper_'+couponid);
	    	
	    	delete this.couponObj[couponid];
	    	ct.remove();
	    	order.upDateUI();
	    },
	    getTotal:function(){
	    	var toTal=0,
	    	    thisCoupon;
	    	for(var couponid in this.couponObj){
	    		thisCoupon=this.couponObj[couponid];
	    		toTal+=thisCoupon.num*thisCoupon.facevalue;
	    	}
	    	return toTal;
	    },
	    setInput:function(){
	       var $input=$('#couponsInput'),
	           thisCoupon,
	           val=[];
	       for(var couponid in this.couponObj){
	    		thisCoupon=this.couponObj[couponid];
	    		if(thisCoupon.num)val.push(couponid+','+thisCoupon.num);
	       }
	       $input.val(val.join(';'));
	    },
	    init:function(o){
	    	var that=this,
	    	    couponCt=$('#coupon-select-ct');
	    	    
	    	order=o;
	    	couponCt.on('click','input',function(){
	    	     var couponid=this.value;
	    	     that.remove(couponid);
	    	});
	    	$('#couponBtn').on('click',function(){
				 var ct=$('#coupon-dialog'),
		             obj={},
				     couponid,
				     num,
				     facevalue,
				     html='',
				     updateUI=function(list){
				     	list.each(function(i,n){
							couponid=$(this).attr('couponid');
							num=$(this).attr('num');
							facevalue=$(this).attr('facevalue');
							obj[couponid]={
							   max:num,
							   facevalue:facevalue,
							   num:1
							}
		                    html+=['<div class="row coupon-select clearfix" id="couponWraper_',couponid,'">\
			         	<p><input type="checkbox" checked value="'+couponid+'"><span>',facevalue,'赢家券</span><label>(剩余<span id="couponlostCt_',couponid,'">',num-1,'</span>张)</label></p>\
			            <div class="col">\
			                <table class="border-table none" rowindex="',i,'">\
			          		<tr><td onclick="coupon.setTicket(-1,',couponid,')">-</td><td id="couponNumCt_',couponid,'">1</td><td onclick="coupon.setTicket(1,',couponid,')">+</td></tr>\
			          		</table>\
			            </div>\
			         </div>'].join('');
						})
						that.couponObj=obj;
						couponCt.html(html);
						order.upDateUI();
				     };
				     
		        ct.show().on('click','li,.long-btn,.mask',function(){
		        	
		            if(this.tagName.toLowerCase()=='li'){
		            	var $li=$(this);
		            	
		               // $li.parent().find('li.selected').removeClass('selected');
						//$li.toggleClass('selected');
		            	updateUI($li);
		            	ct.off('click').hide();
		            }else{
						ct.off('click').hide();
						//updateUI(ct.find('li.selected'));
		            }
		        });
		    });
	    }
    }
});